package servlet;

import util.DButil;
import util.staff;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Calculate extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取客户端请求的参数，并进行判断。
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        request.setCharacterEncoding("utf-8");
        String id=request.getParameter("id").trim();//取得用户密码参数

        double day=Double.parseDouble(request.getParameter("mwork").trim());
        double money=Double.parseDouble(request.getParameter("mbenefit").trim());

        double bmoney=Double.parseDouble(request.getParameter("minsurance").trim());
        staff sstaff = new staff();
        double x = 0;
        try{
            DButil dButil = new DButil();
            Connection con = dButil.getConnection();

            String sql = "select * from staff where id = '"+ id +"'";
            String str = "";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);

            if (rs.next()) {
                sstaff.setWyear(rs.getDouble("wyear"));
                sstaff.setWage(rs.getDouble("wage"));
                double bp = sstaff.getWage();
                double wa = sstaff.getWyear();
                x = (bp + 10 * day + money * wa/ 100) * 0.9 - bmoney;
                BigDecimal bd = new BigDecimal(x);
                bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);

                str = bd + " 元";
            }
            else
                str = "该工程师ID不存在！";
            HttpSession session =  request.getSession();
            session.setAttribute("money", str);

            RequestDispatcher dispatcher=
                    request.getRequestDispatcher("result_wage.jsp");//转发
            dispatcher.forward(request,response);



        }catch(Exception e){
            e.printStackTrace();

        }


    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
